$(function(){
	$('#tt').datagrid({
		url:'/cabinetConfig/getCabinetConfigListPage'
	});
	
});

function doAdd(){
	$('#addForm').form('clear');
	$('#addWin').window('open');
}

function doEdit(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	$('#addWin').window('open');
	$('#idId').val(record.id);
	$('#locationId').val(record.location);
	$('#ipSectionId').val(record.ipSection);
}

function doDel(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	$.messager.confirm('提示', '你确定要删除记录吗', function(opt){
		if (opt){
			var cabinetConfigId=record.id;
			$.ajax({
				url: '/cabinetConfig/deleteCabinetConfig',
				type: 'POST',
				data : {
		            id:cabinetConfigId,
		            delete_flag:1
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "删除成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	})
}

function doSearch(){
	var location=$('#slocationId').val();
	var ipSection=$('#sipSectionId').val();
	$('#tt').datagrid({
		url:'/cabinetConfig/getCabinetConfigListPage',
		queryParams:{
			location:location,
			ipSection:ipSection
		}
	});
	$('#tt').datagrid('unselectAll');
}

function save(){
	var id=$('#idId').val();
	var url='/cabinetConfig/updateCabinetConfig';
	if(id==null||id==''){
		url='/cabinetConfig/saveCabinetConfig';
	}
	
	$('#addForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			var ipSection=$('#ipSectionId').val();
			var isIp=validateIpSeption(ipSection);
			if(!isIp){
				$.messager.alert("提示","IP段输入格式有误");
				return false;
			}
			
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addWin").window('close');
				$('#tt').datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
	
}

function cancel(){
	$('#addWin').window('close');
}


function validateIpSeption(input){
	var dataArr=input.split(".");
	var length=dataArr.length;
	if(input.endsWith(".")){
		length=dataArr.length-1;
	}
	var reg=/^(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])$/;
	for(var i=0;i<length;i++){
		if(!reg.test(dataArr[i])){
			return false;
		}
	}
	return true;
}
